package com.carl.mall.coupon.service.impl;

import com.carl.common.dto.MemberPrice;
import com.carl.common.dto.SkuReductionDTO;
import com.carl.mall.coupon.convert.CouponConvert;
import com.carl.mall.coupon.entity.MemberPriceEntity;
import com.carl.mall.coupon.entity.SkuLadderEntity;
import com.carl.mall.coupon.service.MemberPriceService;
import com.carl.mall.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carl.common.utils.PageUtils;
import com.carl.common.utils.Query;

import com.carl.mall.coupon.dao.SkuFullReductionDao;
import com.carl.mall.coupon.entity.SkuFullReductionEntity;
import com.carl.mall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {


    @Autowired
    private SkuFullReductionService skuFullReductionService;


    @Autowired
    private SkuLadderService skuLadderService;


    @Autowired
    private MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存满减,折扣和会员价的相关信息
     * @param skuReductionDTO
     */
    @Override
    public void saveSkuReduction(SkuReductionDTO skuReductionDTO) {

        BigDecimal price = null;
        for (MemberPrice memberPrice : skuReductionDTO.getMemberPrice()) {
             price = memberPrice.getPrice();
        }
        //mall_sms服务中 保存满减信息sms_sku_full_reduction，折扣sms_sku_ladder，会员价sms_member_price
        /*
         * 满多少件打几折
         */
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        if (skuReductionDTO.getFullCount() > 0 && skuReductionDTO.getFullPrice().compareTo(new BigDecimal(0)) > 0){
        skuLadderEntity.setSkuId(skuReductionDTO.getSkuId());
        skuLadderEntity.setDiscount(skuReductionDTO.getDiscount());
        skuLadderEntity.setFullCount(skuReductionDTO.getFullCount());
        skuLadderEntity.setAddOther(skuReductionDTO.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }
        /*
         * 满多少元打几折
         */
        /////////////////////////////////////////////////////
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        skuFullReductionEntity.setSkuId(skuReductionDTO.getSkuId());
        if (skuReductionDTO.getFullPrice().compareTo(new BigDecimal(0)) > 0 && skuReductionDTO.getFullCount()>0){
        skuFullReductionEntity.setFullPrice(skuReductionDTO.getFullPrice());
        skuFullReductionEntity.setReducePrice(skuReductionDTO.getReducePrice());
        skuFullReductionEntity.setAddOther(skuReductionDTO.getCountStatus());
            skuFullReductionService.save(skuFullReductionEntity);
        }
        /*
         * 会员价
         */
        if (skuReductionDTO.getMemberPrice() != null && !skuReductionDTO.getMemberPrice().isEmpty() && price.compareTo(new BigDecimal(0))>0){
            List<MemberPriceEntity> memberPriceEntities = skuReductionDTO.getMemberPrice().stream().map((member) -> {
                MemberPriceEntity  memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setAddOther(1); //是否可叠加 前端暂时没有
                memberPriceEntity.setSkuId(skuReductionDTO.getSkuId());
                memberPriceEntity.setMemberLevelId(member.getId());
                memberPriceEntity.setMemberLevelName(member.getName());
                memberPriceEntity.setMemberPrice(member.getPrice());
                return memberPriceEntity;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntities);
        }


    }














}